package top.csjss.hospital.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.csjss.hospital.dto.ReportDTO;
import top.csjss.hospital.entity.Report;
import top.csjss.hospital.entity.ReportExample;
import top.csjss.hospital.enums.HospitalExceptionEnums;
import top.csjss.hospital.exception.HospitalException;
import top.csjss.hospital.mapper.ReportMapper;
import top.csjss.hospital.service.ReportService;
import top.csjss.hospital.vo.CashierVO;
import top.csjss.hospital.vo.ReportVO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author ztf
 * @Date 22:14 2021/10/19
 **/
@Service
public class ReportServiceImpl implements ReportService {
    private Logger log = LoggerFactory.getLogger(ReportServiceImpl.class);

    /**
     * 已删除
     */
    private final int DELETE = 0;
    /**
     * 预约成功
     */
    private final int SUCCESS = 1;

    @Autowired
    private ReportMapper reportMapper;

    @Override
    public Integer findReportCountTotal() {
        //封装查询条件
        ReportExample example = new ReportExample();
        ReportExample.Criteria criteria = example.createCriteria();
        criteria.andStateNotEqualTo(DELETE);
        long count = reportMapper.countByExample(example);

        //返回
        return Integer.valueOf(count + "");
    }

    @Override
    public Integer findReportCountCurrent() {
        long count = reportMapper.findReportCountByCurrent();
        return Integer.valueOf(count + "");
    }

    @Override
    public List<ReportVO> findPatientInfo(Integer cc, String name) {
        //调用mapper
        List<Report> reportList = reportMapper.findPatientInfo(cc, name);

        //封装数据
        List<ReportVO> data = new ArrayList<>();
        for (Report report : reportList) {
            ReportVO vo = new ReportVO();
            BeanUtils.copyProperties(report, vo);
            data.add(vo);
        }
        return data;
    }

    @Override
    public Integer checkPhoneAndCarid(String phone, String carid) {
        // 调用Service查询手机号/身份证是否重复
        int count = reportMapper.checkExists("phone", phone);
        if (count >= 1) {
            return 1;
        }
        count = reportMapper.checkExists("carid", carid);
        return count >= 1 ? 2 : 3;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void addReport(ReportDTO reportDTO) {
        //DTO->entity
        Report report = new Report();
        BeanUtils.copyProperties(reportDTO, report);
        report.setDepartment(reportDTO.getDepartmentId());
        report.setDoctor(reportDTO.getDoctorId());
        report.setReportType(reportDTO.getRegisterTypeId());

        //封装state和time
        report.setTime(new Date());
        report.setState(SUCCESS);

        //添加
        int count = reportMapper.insertSelective(report);
        //校验
        if (count != 1) {
            log.error("【添加患者信息】 添加患者信息失败！  report = {}", report);
            throw new HospitalException(HospitalExceptionEnums.REPORT_ADD_ERROR);
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteReport(Integer id) {
        //查询该用户
        Report report = reportMapper.selectByPrimaryKey(id);
        report.setState(0);
        //封装校验数据
        ReportExample example = new ReportExample();
        ReportExample.Criteria criteria = example.createCriteria();
        criteria.andReportIdEqualTo(id);
        reportMapper.updateByExampleSelective(report, example);
    }

    @Override
    public PageInfo<Report> displayPatientInformation(String name, Integer page, Integer limit) {
        //1. 分页
        PageHelper.startPage(page, limit);
        //2. 执行查询
        List<Report> list = reportMapper.findPrescriptionInfo(name);
        //3. 封装PageInfo
        PageInfo<Report> pageInfo = new PageInfo<>(list);
        //4. 返回
        return pageInfo;
    }

    @Override
    public String selectCause(Integer reid) {
        Report report = reportMapper.selectByPrimaryKey(reid);
        return report.getZhuan();
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void transferToHospital(Integer id, String zhuan) {
        Report report = new Report();
        report.setZhuan(zhuan);
        report.setState(4);
        //封装查询数据
        ReportExample example = new ReportExample();
        ReportExample.Criteria criteria = example.createCriteria();
        criteria.andReportIdEqualTo(id);
        Integer count = reportMapper.updateByExampleSelective(report, example);
        if (count!=1){
            log.info("【转院操作】转院失败");
            throw new HospitalException(HospitalExceptionEnums.TRANSFER_TO_HOSPITAL);
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateEtiology(Integer reid, String bing) {
        Report report = new Report();
        report.setZhuan(bing);
        //封装查询数据
        ReportExample example = new ReportExample();
        ReportExample.Criteria criteria = example.createCriteria();
        criteria.andReportIdEqualTo(reid);
        Integer count = reportMapper.updateByExampleSelective(report, example);
        if (count!=1){
            log.info("【修改病因】修改失败");
            throw new HospitalException(HospitalExceptionEnums.CAO_UPDATE_ETIOLOGY);
        }
    }


}
